package com.myres.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.db.ConnectionPool;
import com.myres.model.Score;
import com.myres.model.Order;
import java.sql.Timestamp;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.myres.util.ScoreTool;
import com.myres.db.ModelService;


public class TransportScore extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public TransportScore() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request,response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request,response);
	}
	public void process(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		Connection con=null;
		try{
		con=ConnectionPool.getPool().getConnection();
		PreparedStatement pstmt=con.prepareStatement("select NO,UID,Payment,OrderTime from res_orders");
		ResultSet rs=pstmt.executeQuery();
		ArrayList<Score> scores=new ArrayList<Score>();
		while(rs.next()){
			Score score=new Score();
			score.setOno(rs.getString(1));
			score.setUid(rs.getInt(2));
			float payment=rs.getFloat(3);
			score.setScore(ScoreTool.parseScore(payment));
			score.setAddTime(rs.getTimestamp(4));
			score.setNote(Score.NOTE_DISCOUNT);
			score.setReason(Score.TYPE_DISCONT);
			scores.add(score);
		}
		con.close();
		new ModelService().insertScore(scores);
		}catch(SQLException e){
			
			
		}finally{
			try{
				con.close();
			}catch(Exception e){
				
			}
		}

}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
